package org.jground.rx.ui.zk;

import java.util.Stack;

import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;
import org.jground.rx.RXSystem;
import org.jground.rx.ui.IMainPanel;
import org.jground.rx.ui.IUIMemberLogin;
import org.jground.rx.ui.pref.PrefService;
import org.jground.rx.web.RXWebUtils;
import org.jground.rx.web.UIProperties;
import org.jground.util.ArrayUtils;
import org.jground.util.StringUtils;
import org.jground.zk.util.ZKUtils;
import org.jground.zk.zulX.HboxX;
import org.jground.zk.zulX.VboxX;
import org.jground.zk.zulX.WindowX;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Page;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zul.Image;
import org.zkoss.zul.Label;
import org.zkoss.zul.Messagebox;

public class UIMasterFrame extends WindowX implements EventListener {

    private static Log logger = LogFactory.getLog(UIMasterFrame.class);

    private Page page;
    private HboxX uiHeader;
    private VboxX uiMainbox;
    private UIFrame uiFrame;
    private IUIMemberLogin<Component, EventListener> uiMemberLogin;

    //
    private Stack<IMainPanel<Component>> stackedDialogs = new Stack<IMainPanel<Component>>();

    // PrefService
    private PrefService prefService;

    // member login
    private String uiMemberLoginClass;

    // UIRX properties

    public UIMasterFrame(Page page) {
        logger.debug("init");
        //
        this.page = page;
        this.setPage(page);
        //
        prefService = new PrefService();
        //
        // COMPONENTS
        //
        uiHeader = new HboxX();
        uiMainbox = new VboxX();
        uiMainbox.setWidth("100%");
        uiMemberLogin = createUIMemberLogin();
        uiFrame = createUIFrame();
        //
        // logo ...
        //
        String logoImgSource = UIProperties.getInstance().getProperty(UIProperties.frame_logo);
        if (StringUtils.isNotBlank(logoImgSource)) {
            Image logo = new Image(logoImgSource);
            // logo.setPage(page);
            uiHeader.add(logo);
        }
        // add uiMemberLogin if available
        if (uiMemberLogin != null) {
            HboxX h = new HboxX();
            uiHeader.add(h);
            h.add(new HboxX(uiMemberLogin.getCommandView()));
        }
        // checking user and role availability ...
        try {
            logger.info(RXSystem.getRXPrincipalMethods().getUserName());
        } catch (Exception e) {
            add(new HboxX(new Label(" *** no user specified - can not continue, sorry *** ")));
            return;
        }
        try {
            logger.info(ArrayUtils.join(RXSystem.getRXPrincipalMethods().getUserRoles()));
        } catch (Exception e) {
            add(new HboxX(new Label(" *** no roles specified - can not continue, sorry *** ")));
            return;
        }
        setStyle("margin-top:4px");
        setBorder("none");
        // 
        // LAYOUT
        //      
        add(uiHeader);
        add(uiMainbox);
        uiMainbox.add(uiFrame);
        //
        // EVENT
        //
        if (uiMemberLogin != null) {
            uiMemberLogin.addEventListener(this);
        }
    }

    private UIFrame createUIFrame() {
        return new UIFrame(this);
    }

    public String getUiProperty(String key) {
        return UIProperties.getInstance().getProperty(key, key);
    }

    public IMainPanel<Component> getCurrentMainPanel() {
        return stackedDialogs.peek();
    }

    public void onEvent(Event event) throws Exception {
        if (event.getTarget() == uiMemberLogin) {
            if (event.getName() == IUIMemberLogin.LOGIN_OK) {
                if (uiFrame != null) {
                    uiFrame.detach();
                }
                uiMainbox.set(uiFrame = createUIFrame());
            }
            if (event.getName() == IUIMemberLogin.OPEN_LOGIN) {
                uiMainbox.set(uiMemberLogin.getView());
            }
            if (event.getName() == IUIMemberLogin.LOGOUT_OK) {
                RXWebUtils.setMinimalPrincipalMethods(ZKUtils.getHttpSession(this));
                Messagebox.show("Thank you for using : "
                        + UIProperties.getInstance().getProperty(UIProperties.browser_title));
                uiMainbox.set(uiFrame = createUIFrame());
            }
            if (event.getName() == IUIMemberLogin.PASSWORD_SEND_OK) {
                RXWebUtils.setMinimalPrincipalMethods(ZKUtils.getHttpSession(this));
                Messagebox.show("New Password was sent!");
                uiMainbox.set(uiFrame = createUIFrame());
            }
            if (event.getName() == IUIMemberLogin.DONE) {
                uiMainbox.set(uiFrame);
            }
            if (event.getName() == IUIMemberLogin.REGISTER_OK) {
                uiMainbox.set(uiFrame = createUIFrame());
            }
        }

    }

    private IUIMemberLogin<Component, EventListener> createUIMemberLogin() {
        IUIMemberLogin<Component, EventListener> ml = null;
        uiMemberLoginClass = UIProperties.getInstance().getProperty(UIProperties.frame_member_login_class);
        if (StringUtils.isNotBlank(uiMemberLoginClass)) {
            try {
                ml = (IUIMemberLogin<Component, EventListener>) Class.forName(uiMemberLoginClass).newInstance();
                ml.init(this);
            } catch (Exception e) {
                logger.error(e, e);
            }
        }
        return ml;
    }

    public PrefService getPrefService() {
        return prefService;
    }

    public Page getMainPage() {
        return page;
    }
}